/**
 * @description 手写 instanceof
 * instanceof的原理就是顺着原型链__proto__一直查找，看是否能和XX.prototype相等
 */

function customInstanceof(instance: any, obj: any): boolean {
    const type = typeof instance;
    if(type !== 'object' && type !== 'function'){
        // 值类型返回false
        return false;
    }

    while(instance) {
        if(instance.__proto__ === obj.prototype) {
            return true;
        }
        instance = instance.__proto__;
    }

    return false;
}

console.log(customInstanceof([], Object));
console.log(customInstanceof('[]', Object));
console.log(customInstanceof([], Array));
console.log(customInstanceof(()=>{}, Function));
console.log(customInstanceof(null, Function));